package de.homelabs.web.homelabs.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import de.homelabs.hlmodules.cms.configuration.service.HLConfigurationManager;
import de.homelabs.hlmodules.cms.realm.service.HLRealmService;

/**
 * Controller for CSS and Javascript files. They are expected to lie under:
 * [configPath]/scripts/[scriptFile]
 * 
 * @author dmueller
 * 
 */
@Controller("scriptController")
public class ScriptController {

	@Autowired
	@Qualifier("hlConfigurationManager")
	HLConfigurationManager configManager;

	@Autowired
	@Qualifier("hlRealmService")
	HLRealmService hlRealmService;

	@RequestMapping({ "*.css", "*.js" })
	void handleScriptRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// configManager.getDefaultConfig().getConfigPath()
		// System.out.println(request.getRequestURI());
		// System.out.println(request.getServletPath());
		String normalizedDomain = hlRealmService.getNormalizedDomain(request);
		System.out.println("Path: "
				+ configManager.getConfigurationByDomain(hlRealmService.getNormalizedDomain(request)).getConfigPath() + "/"
				+ normalizedDomain + "/scripts" + request.getServletPath());
		File file = new File(configManager.getConfigurationByDomain(hlRealmService.getNormalizedDomain(request)).getConfigPath()
				+ "/" + normalizedDomain + "/scripts"
				+ request.getServletPath());

		if (request.getServletPath().contains(".css")) {
			response.setContentType("text/css");
		} else if (request.getServletPath().contains(".js")) {
			response.setContentType("text/javascript");
		}
		response.setContentLength((int) file.length());
		OutputStream os = response.getOutputStream();
		IOUtils.copy(new FileInputStream(file), os);

		// debug
		// System.out.println(request.getPathInfo());
		// System.out.println(request.getRequestURI());
		// System.out.println(request.getRequestURL());
	}

	@RequestMapping({ "**/*.jpg", "*.jpg", "**/*.png", "*.png", "**/*.ico",
			"*.ico" })
	void handleImageRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		System.out.println("Image: " + request.getServletPath());
		String normalizedDomain = hlRealmService.getNormalizedDomain(request);
		File file = new File(configManager.getConfigurationByDomain(hlRealmService.getNormalizedDomain(request)).getConfigPath()
				+ "/" + normalizedDomain + "/images" + request.getServletPath());

		if (request.getServletPath().contains(".jpg")) {
			response.setContentType("image/jpg");
		} else if (request.getServletPath().contains(".png")) {
			response.setContentType("image/png");
		} else {
			// do nothing
			return;
		}
		response.setContentLength((int) file.length());
		OutputStream os = response.getOutputStream();
		IOUtils.copy(new FileInputStream(file), os);
	}
}
